package main;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * handle global parameters of mission
 * @author lefuneste
 *
 */
public class Prop {
	private static Properties prop;

	/**
	 * Read constants and parameters from default.properties file and overwite
	 * them with current.properties if needed
	 * 
	 * @throws IOException
	 */
	public static void load() throws IOException {
		// create and load default properties
		Properties defaultProps = new Properties();
		try (FileInputStream in = new FileInputStream("default.properties")) {
			defaultProps.load(in);
			in.close();

			// create application properties with default
			prop = new Properties(defaultProps);

			// now load properties
			// from last invocation
			try (FileInputStream in2 = new FileInputStream("current.properties")) {
				prop.load(in2);
				in2.close();
			}
		}
	}

	/**
	 * Save the modified properties to the current.properties file
	 * 
	 * @throws IOException
	 */
	public static void save() throws IOException {
		// create and load default properties
		try (FileOutputStream out = new FileOutputStream("current.properties")) {
			prop.store(out, "- non default settings -");
			out.close();
		}
	}

	/**
	 * Return the value of a propertie
	 * 
	 * @param key
	 *            : propertie name
	 * @return String: propertie value
	 */
	public static String get(String key) {
		return prop.getProperty(key);
	}

	/**
	 * Set a value to a propertie
	 * 
	 * @param key
	 *            propertie name
	 * @param value
	 *            propertie value
	 */
	public static void set(String key, String value) {
		prop.setProperty(key, value);
	}
	
	/**
	 * Set a value to a propertie
	 * 
	 * @param key
	 *            propertie name
	 * @param value
	 *            propertie value
	 */	
	public static void set(String key, int value) {
		prop.setProperty(key, Integer.toString(value));
	}
	
	/**
	 * Set a value to a propertie
	 * 
	 * @param key
	 *            propertie name
	 * @param value
	 *            propertie value
	 */	
	public static void set(String key, double value) {
		prop.setProperty(key, Double.toString(value));
	}
	
	/**
	 * Set a value to a propertie
	 * 
	 * @param key
	 *            propertie name
	 * @param value
	 *            propertie value
	 */
	public static void set(String key, boolean value) {
		prop.setProperty(key, Boolean.toString(value));
	}

}
